package com.ruoyi.system.service.award.competition.impl;

import java.util.List;

import com.ruoyi.system.domain.award.competition.CompetitionAwardInfo;
import com.ruoyi.system.domain.award.competition.CompetitionAwardTeacher;
import com.ruoyi.system.domain.award.competition.CompetitionAwardTeamMembers;
import com.ruoyi.system.domain.award.competition.CompetitionAwardWorks;
import com.ruoyi.system.mapper.award.competition.CompetitionAwardInfoMapper;
import com.ruoyi.system.mapper.award.competition.CompetitionAwardTeacherMapper;
import com.ruoyi.system.mapper.award.competition.CompetitionAwardTeamMembersMapper;
import com.ruoyi.system.mapper.award.competition.CompetitionAwardWorksMapper;
import com.ruoyi.system.service.award.competition.ICompetitionAwardInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 竞赛获奖信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-28
 */
@Service
public class CompetitionAwardInfoServiceImpl implements ICompetitionAwardInfoService
{
    @Autowired
    private CompetitionAwardInfoMapper competitionAwardInfoMapper;
    @Autowired
    private CompetitionAwardWorksMapper competitionAwardWorksMapper;
    @Autowired
    private CompetitionAwardTeacherMapper competitionAwardTeacherMapper;
    @Autowired
    private CompetitionAwardTeamMembersMapper competitionAwardTeamMembersMapper;


    /**
     * 查询竞赛获奖信息
     * 
     * @param id 竞赛获奖信息主键
     * @return 竞赛获奖信息
     */
    @Override
    public CompetitionAwardInfo selectCompetitionAwardInfoById(Long id)
    {
        CompetitionAwardInfo info = competitionAwardInfoMapper.selectCompetitionAwardInfoById(id);
        CompetitionAwardWorks competitionAwardWorks = new CompetitionAwardWorks();
        competitionAwardWorks.setAwardInfoId(info.getId());
        List<CompetitionAwardWorks> works = competitionAwardWorksMapper.selectCompetitionAwardWorksList(competitionAwardWorks);
        if(works != null){
            // 查询老师信息  查询团队成员信息
            for (CompetitionAwardWorks work : works) {
                CompetitionAwardTeacher competitionAwardTeacher = new CompetitionAwardTeacher();
                competitionAwardTeacher.setWorkId(work.getId());
                List<CompetitionAwardTeacher> teachers = competitionAwardTeacherMapper.selectCompetitionAwardTeacherList(competitionAwardTeacher);
                work.setTeachers(teachers);
                CompetitionAwardTeamMembers competitionAwardTeamMembers = new CompetitionAwardTeamMembers();
                competitionAwardTeamMembers.setWorkId(work.getId());
                List<CompetitionAwardTeamMembers> teamMembers = competitionAwardTeamMembersMapper.selectCompetitionAwardTeamMembersList(competitionAwardTeamMembers);
                work.setTeamMembers(teamMembers);
            }
        }
        info.setWorks(works);
        return info;
    }

    /**
     * 查询竞赛获奖信息列表（包含作品、老师和学生信息）
     *
     * @param competitionAwardInfo 竞赛获奖信息
     * @return 竞赛获奖信息列表
     */
    @Override
    public List<CompetitionAwardInfo> selectCompetitionAwardInfoList(CompetitionAwardInfo competitionAwardInfo) {
        List<CompetitionAwardInfo> competitionAwardInfoList = competitionAwardInfoMapper.selectCompetitionAwardInfoList(competitionAwardInfo);
        for (CompetitionAwardInfo info : competitionAwardInfoList) {
            // 查询作品信息
            CompetitionAwardWorks competitionAwardWorks = new CompetitionAwardWorks();
            competitionAwardWorks.setAwardInfoId(info.getId());
            List<CompetitionAwardWorks> works = competitionAwardWorksMapper.selectCompetitionAwardWorksList(competitionAwardWorks);
            info.setWorks(works);

            // 查询老师信息  查询团队成员信息
            for (CompetitionAwardWorks work : works) {
                CompetitionAwardTeacher competitionAwardTeacher = new CompetitionAwardTeacher();
                competitionAwardTeacher.setWorkId(work.getId());
                List<CompetitionAwardTeacher> teachers = competitionAwardTeacherMapper.selectCompetitionAwardTeacherList(competitionAwardTeacher);
                work.setTeachers(teachers);
                CompetitionAwardTeamMembers competitionAwardTeamMembers = new CompetitionAwardTeamMembers();
                competitionAwardTeamMembers.setWorkId(work.getId());
                List<CompetitionAwardTeamMembers> teamMembers = competitionAwardTeamMembersMapper.selectCompetitionAwardTeamMembersList(competitionAwardTeamMembers);
                work.setTeamMembers(teamMembers);
            }
        }
        return competitionAwardInfoList;
    }
    /**
     * 新增竞赛获奖信息
     * 
     * @param competitionAwardInfo 竞赛获奖信息
     * @return 结果
     */
    @Override
    public int insertCompetitionAwardInfo(CompetitionAwardInfo competitionAwardInfo)
    {
        int result = competitionAwardInfoMapper.insertCompetitionAwardInfo(competitionAwardInfo);
        if (result == 1) {
            // 插入成功，返回主键ID
            Long id = competitionAwardInfo.getId();
            // 插入竞赛获奖作品信息
            // 2. 处理作品数据
            if (competitionAwardInfo.getWorks() != null && !competitionAwardInfo.getWorks().isEmpty()) {
                for (CompetitionAwardWorks work : competitionAwardInfo.getWorks()) {
                    // 设置作品与竞赛的关联
                    work.setAwardInfoId(id);

                    // 保存作品
                    competitionAwardWorksMapper.insertCompetitionAwardWorks(work);

                    // 3. 处理指导老师数据
                    if (work.getTeachers() != null && !work.getTeachers().isEmpty()) {
                        for (CompetitionAwardTeacher teacher : work.getTeachers()) {
                            teacher.setWorkId(work.getId());
                            competitionAwardTeacherMapper.insertCompetitionAwardTeacher(teacher);
                        }
                    }

                    // 4. 处理团队成员数据
                    if (work.getTeamMembers() != null && !work.getTeamMembers().isEmpty()) {
                        for (CompetitionAwardTeamMembers member : work.getTeamMembers()) {
                            member.setWorkId(work.getId());
                            competitionAwardTeamMembersMapper.insertCompetitionAwardTeamMembers(member);
                        }
                    }
                }
            }
        }
        return result;
    }

    /**
     * 修改竞赛获奖信息
     * 
     * @param competitionAwardInfo 竞赛获奖信息
     * @return 结果
     */
    @Override
    public int updateCompetitionAwardInfo(CompetitionAwardInfo competitionAwardInfo)
    {
        return competitionAwardInfoMapper.updateCompetitionAwardInfo(competitionAwardInfo);
    }

    /**
     * 批量删除竞赛获奖信息
     * 
     * @param ids 需要删除的竞赛获奖信息主键
     * @return 结果
     */
    @Override
    public int deleteCompetitionAwardInfoByIds(Long[] ids)
    {
        // 1. 先删除关联的团队成员信息
        for (Long id : ids) {
            // 查询该竞赛下的所有作品
            CompetitionAwardWorks worksQuery = new CompetitionAwardWorks();
            worksQuery.setAwardInfoId(id);
            List<CompetitionAwardWorks> works = competitionAwardWorksMapper.selectCompetitionAwardWorksList(worksQuery);

            if (works != null && !works.isEmpty()) {
                for (CompetitionAwardWorks work : works) {
                    // 删除作品关联的团队成员
                    CompetitionAwardTeamMembers memberQuery = new CompetitionAwardTeamMembers();
                    memberQuery.setWorkId(work.getId());
                    List<CompetitionAwardTeamMembers> members= competitionAwardTeamMembersMapper.selectCompetitionAwardTeamMembersList(memberQuery);
                    if(members != null && !members.isEmpty()){
                        for(CompetitionAwardTeamMembers member : members){
                            competitionAwardTeamMembersMapper.deleteCompetitionAwardTeamMembersById(member.getId());
                        }
                    }
                    // 删除作品关联的指导老师
                    CompetitionAwardTeacher teacherQuery = new CompetitionAwardTeacher();
                    teacherQuery.setWorkId(work.getId());
                    List<CompetitionAwardTeacher> teachers = competitionAwardTeacherMapper.selectCompetitionAwardTeacherList(teacherQuery);
                    if(teachers != null && !teachers.isEmpty()){
                        for(CompetitionAwardTeacher teacher : teachers){
                            competitionAwardTeacherMapper.deleteCompetitionAwardTeacherById(teacher.getId());
                        }
                    }
                    // 2. 删除关联的作品信息
                    competitionAwardWorksMapper.deleteCompetitionAwardWorksById(work.getId());
                }

            }
        }
        // 3. 最后删除竞赛获奖主信息
        return competitionAwardInfoMapper.deleteCompetitionAwardInfoByIds(ids);
    }

    /**
     * 删除竞赛获奖信息信息
     * 
     * @param id 竞赛获奖信息主键
     * @return 结果
     */
    @Override
    public int deleteCompetitionAwardInfoById(Long id)
    {
        return competitionAwardInfoMapper.deleteCompetitionAwardInfoById(id);
    }
}
